using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic;
using System.Web;
using System.Web.Mvc;
using SDT.Model;
using SDT.Lib.Paging;
using SDT.Site.ViewModels;
using SDT.Site.Properties;
using System.Linq.Dynamic;
using SDT.Lib.Common;
using System.Web.Security;
namespace SDT.Site.Controllers
{
    public class ProjectController : Controller
    {
        private string order = "AddDate desc";
        SDTDataContext eDataContext = new SDTDataContext();
        //
        // GET: /Project/
        public ActionResult Index()
        {
            ProjectIndexModels p = new ProjectIndexModels();
            p.Categories = eDataContext.ProjectCategories.Where(e=>e.ActiveYN==true).OrderBy(string.Format("{0} {1}", "Priority", "asc")).ToList();
            p.Cities = eDataContext.Cities.OrderBy(string.Format("{0} {1}", "Name", "asc")).ToList();
            p.Districts = new List<District>();
            p.ImageUrl = Settings.Default.SiteRootUrl + Settings.Default.ProjectRootUrl;
            p.ImagePath = Settings.Default.SiteRootPath + Settings.Default.ProjectRootPath;
            return View(p);
        }
        [ChildActionOnly]
        public ActionResult GetTop(int number, int catid, int hotYN, int pubYN, string title, string order, string orderDir)
        {
            var l = ProjectManager.GetList(number, catid, hotYN, pubYN, order, orderDir);
            ProjectListModels p = new ProjectListModels();
            p.Projects = l;
            p.ImageUrl = Settings.Default.SiteRootUrl + Settings.Default.ProjectRootUrl;
            p.ImagePath = Settings.Default.SiteRootPath + Settings.Default.ProjectRootPath;
            ViewData["Title"] = title;
            return View(p);
        }
        [ChildActionOnly]
        public ActionResult GetByCat(int number, int catid, int hotYN, int pubYN, string title, string order, string orderDir)
        {
            var p = new ProjectListModels();
            var l = ProjectManager.GetList(number, catid, hotYN, pubYN, order, orderDir);
            p.Projects = l;
            p.ImageUrl = Settings.Default.SiteRootUrl + Settings.Default.ProjectRootUrl;
            p.ImagePath = Settings.Default.SiteRootPath + Settings.Default.ProjectRootPath;
            ViewData["Title"] = title;
            p.Category = eDataContext.ProjectCategories.Where(e => e.ID == catid).SingleOrDefault();
            return View(p);
        }
        public ActionResult List(int? id, int? cityid, int? distrid, int? page)
        {
            if (page == null) page = 1; ;
            var pageIndex = page.Value - 1;
            var pageSize = Convert.ToInt32(UserState.getUserStateFromRequest("project.PageSize", "pageSize", Settings.Default.ProductPageSize));
            string order = UserState.getUserStateFromRequest("project.order", "order", "ID").ToString();
            string orderDir = UserState.getUserStateFromRequest("project.orderDir", "orderDir", "desc").ToString();

            ProjectIndexModels p = new ProjectIndexModels();
            p.ImageUrl = Settings.Default.SiteRootUrl + Settings.Default.ProjectRootUrl;
            p.ImagePath = Settings.Default.SiteRootPath + Settings.Default.ProjectRootPath;

            p.Category = id.HasValue && id.Value > 0 ? eDataContext.ProjectCategories.Where(e => e.ID == id).SingleOrDefault() : null;
            p.City = cityid.HasValue && cityid.Value > 0 ? eDataContext.Cities.Where(e => e.ID == cityid).SingleOrDefault() : null;
            p.District = distrid.HasValue && distrid.Value > 0 ? eDataContext.Districts.Where(e => e.ID == distrid).SingleOrDefault() : null;

            p.CityID = cityid.HasValue ? cityid.Value : 0;
            p.DistrictID = distrid.HasValue ? distrid.Value : 0;
            p.Keyword = Request["key"] != null ? Request["key"] : string.Empty;
           
            // Added by Vietlv (16/09/2011)
            var companyID = Request["companyID"] != null && Request["companyID"].Length > 0 ? Convert.ToInt32(Request["companyID"]) : 0;
            if (companyID > 0)
            {
                p.Projects = ProjectManager.GetListOfCompany(id, companyID, cityid, distrid, p.Keyword, 0, 1, order, orderDir, pageIndex, pageSize);
            }
            else
            {
                p.Projects = ProjectManager.GetList(id, cityid, distrid, p.Keyword, 0, 1, order, orderDir, pageIndex, pageSize);
            }
            return View(p);
        }
        public ActionResult Company(int id, int? page)
        {
            if (page == null) page = 1; ;
            var pageIndex = page.Value - 1;
            var pageSize = Convert.ToInt32(UserState.getUserStateFromRequest("project.PageSize", "pageSize", Settings.Default.ProductPageSize));
            string order = UserState.getUserStateFromRequest("project.order", "order", "ID").ToString();
            string orderDir = UserState.getUserStateFromRequest("project.orderDir", "orderDir", "desc").ToString();

            ProjectIndexModels p = new ProjectIndexModels();
            p.ImageUrl = Settings.Default.SiteRootUrl + Settings.Default.ProjectRootUrl;
            p.ImagePath = Settings.Default.SiteRootPath + Settings.Default.ProjectRootPath;

            p.Category = null;
            p.City =  null;
            p.District = null;

            p.CityID = 0;
            p.DistrictID = 0;
            p.Keyword = Request["key"] != null ? Request["key"] : string.Empty;

            p.Company = eDataContext.Companies.Where(e => e.ID == id).SingleOrDefault();
            p.Projects = ProjectManager.GetList(id, 0, 0,id, p.Keyword, 0, 1, order, orderDir, pageIndex, pageSize);
            return View(p);
        }
        //
        // GET: /Project/Details/5

        public ActionResult Details(int id)
        {
            ProjectDetailModels p = new ProjectDetailModels();
            p.Project = eDataContext.Projects.Where(pr => pr.ID == id).FirstOrDefault();
            p.ImageUrl = Settings.Default.SiteRootUrl + Settings.Default.ProjectRootUrl;
            p.ImagePath = Settings.Default.SiteRootPath + Settings.Default.ProjectRootPath;
            p.ProjectImageTypies = eDataContext.ProjectImageTypes.OrderBy(string.Format("{0} {1}", "ID", "asc")).ToList();

            p.CompanyImageUrl = Settings.Default.SiteRootUrl + Settings.Default.CompanyRootUrl;
            p.CompanyImagePath = Settings.Default.SiteRootPath + Settings.Default.CompanyRootPath;
            return View(p);

        }
        [ChildActionOnly]
        public ActionResult Search(int id, int cityid, int distrid)
        {
            ProjectIndexModels p = new ProjectIndexModels();
            p.Categories = eDataContext.ProjectCategories.OrderBy(string.Format("{0} {1}", "Priority", "asc")).ToList();
            p.Cities = eDataContext.Cities.OrderBy(string.Format("{0} {1}", "Name", "asc")).ToList();
            p.Districts = cityid > 0 ? eDataContext.Districts.Where(e => e.CityID == cityid).ToList() : new List<District>();
            p.ImageUrl = Settings.Default.SiteRootUrl + Settings.Default.ProjectRootUrl;
            p.ImagePath = Settings.Default.SiteRootPath + Settings.Default.ProjectRootPath;

            p.Category = id > 0 ? eDataContext.ProjectCategories.Where(e => e.ID == id).SingleOrDefault() : null;
            p.City = cityid > 0 ? eDataContext.Cities.Where(e => e.ID == cityid).SingleOrDefault() : null;
            p.District = distrid > 0 ? eDataContext.Districts.Where(e => e.ID == distrid).SingleOrDefault() : null;

            p.CityID = cityid;
            p.DistrictID = distrid;
            p.Keyword = Request["key"] != null ? Request["key"] : string.Empty;
            return View(p);
        }
        [HttpPost]
        public ActionResult Search()
        {
            ProjectIndexModels p = new ProjectIndexModels();
            var catID = Utils.ParseInt(Request["ddlCategory"], 0);
            var cityID = Utils.ParseInt(Request["ddlCity"], 0);
            var districtID = Utils.ParseInt(Request["ddlDistrict"], 0);
            var keyword = Request["txtKeyword"] != null ? Request["txtKeyword"] : string.Empty;
            if (keyword != string.Empty)
            {
                return RedirectToAction("list", "project", new { id = catID, cityid = cityID, distrid = districtID, key = keyword });
            }
            else {
                return RedirectToAction("list", "project", new { id = catID, cityid = cityID, distrid = districtID });
            }
        }
        [ChildActionOnly]
        public ActionResult Hot(int number, int catid, int hotYN, int pubYN, string title, string prjorder, string prjorderDir)
        {
            var l = ProjectManager.GetList(number, catid, hotYN, pubYN, prjorder, prjorderDir);
            ViewData["ProjectRootUrl"] = Settings.Default.SiteRootUrl + Settings.Default.ProjectRootUrl;
            ProjectListModels p = new ProjectListModels();
            p.Projects = l;
            p.ImageUrl = Settings.Default.SiteRootUrl + Settings.Default.ProjectRootUrl;
            p.ImagePath = Settings.Default.SiteRootPath + Settings.Default.ProjectRootPath;
            ViewData["Title"] = title;
            return View(p);
        }
        [ChildActionOnly]
        public ActionResult HotByCat(int number, int catid, int hotYN, int pubYN, string title, string prjorder, string prjorderDir)
        {
            var l = ProjectManager.GetList(number, catid, hotYN, pubYN, prjorder, prjorderDir);
            ProjectListModels p = new ProjectListModels();
            p.Projects = l;
            p.ImageUrl = Settings.Default.SiteRootUrl + Settings.Default.ProjectRootUrl;
            p.ImagePath = Settings.Default.SiteRootPath + Settings.Default.ProjectRootPath;
            ViewData["Title"] = title;
            return View(p);
        }
        [ChildActionOnly]
        public ActionResult Highlights()
        {
            ProjectListModels p = new ProjectListModels();
            p.ProjectLegals = eDataContext.ProjectLegalStatus.ToList();
            p.ImageUrl = Settings.Default.SiteRootUrl + Settings.Default.ProjectRootUrl;
            p.ImagePath = Settings.Default.SiteRootPath + Settings.Default.ProjectRootPath;
            return View(p);
        }

        /// <summary>
        /// Function Name: OtherItems
        /// Description: Get other items in project have catid = catid of current article
        /// </summary>
        /// <param name="id"></param>
        /// <param name="number"></param>
        /// <param name="title"></param>
        /// <param name="greaterYN"></param>
        /// <returns></returns>
        [ChildActionOnly]
        public ActionResult OtherItems(int id, int number, string title)
        {
            var a = new ProjectListModels();
            var p = eDataContext.Projects.Where(e => e.ID == id).SingleOrDefault();
            var l = eDataContext.Projects.Where(e => e.CatID == p.CatID)
                .Where(e => e.PubYN == true)
                .Where(e => e.ID != id)
                .OrderByDescending(e => e.ID).Take(number).ToList(); ;
            ViewData["Title"] = title;
            a.Projects = l;
            a.Category = p.ProjectCategory;
            return View(a);
        }
        [ChildActionOnly]
        public ActionResult OtherInCompany(int id, int number, string title)
        {
            var a = new ProjectListModels();
            var p = eDataContext.Projects.Where(e => e.ID == id).SingleOrDefault();
            var c = p.ProjectCompanyMaps.Where(m => m.TypeID == ProjectCompanyListType.Investors).ToList();
            var companyID = c != null && c.Count > 0 ? c[0].CompanyID : 0;
            var l = eDataContext.Projects.Where(e => e.ProjectCompanyMaps.Where(k => k.TypeID == ProjectCompanyListType.Investors && k.CompanyID == companyID).ToList().Count > 0)
                .Where(e => e.PubYN == true)
                .Where(e => e.ID != id)
                .OrderByDescending(e => e.ID).Take(number).ToList(); ;
            ViewData["Title"] = title;
            a.Projects = l;
            a.Category = p.ProjectCategory;
            return View(a);
        }
        [ChildActionOnly]
        public ActionResult ArticlesInProject(int id, int number, string title)
        {
            var a = new ArticleListViewModel();
            var p = eDataContext.Projects.Where(e => e.ID == id).SingleOrDefault();
            var l = eDataContext.Articles.Where(e => e.ArticleProjectMaps.Where(k => k.ProjectID == p.ID).ToList().Count > 0)
                .Where(e => e.PubYN == true)
                .Where(e => e.ID != id)
                .OrderByDescending(e => e.ID).Take(number).ToList(); ;
            ViewData["Title"] = title;
            a.Articles = l;
            return View(a);
        }
        [ChildActionOnly]
        public ActionResult ProductsInProject(int id, string title)
        {
            var obj = new ProductsInProjectModels();
            obj.Project = eDataContext.Projects.Where(e => e.ID == id).SingleOrDefault();
            obj.ProductCategories = eDataContext.ProductCategories
                .Where(e => e.Products.Where(k => k.ProjectID == id).ToList().Count > 0)
                .Where(e => e.ActiveYN == true)
                .OrderByDescending(e => e.Priority).ToList();
            ViewData["Title"] = title;
            return View(obj);
        }
        [ChildActionOnly]
        public ActionResult LatestComments(int id, int number)
        {
            var latestC = new List<Comment>();
            try
            {
                latestC = ProjectManager.GetComments(id, true, number).ToList();
            }
            catch (Exception exp)
            {
                ErrorLog.WriteLog(exp);
            }
            return PartialView(latestC);
        }
        [ChildActionOnly]
        public ActionResult Comment(int id)
        {
            CommentEditModels c = new CommentEditModels();
            c.ObjectID = id;
            var u = Membership.GetUser();
            if (u != null)
            {
                c.FullName = u.UserName;
                c.Email = u.Email;
            }
            return PartialView(c);
        }       
        

        /// <summary>
        /// Author: Takamaru Nguyen
        /// Date: 11/08/2011
        /// </summary>
        public ActionResult Tag(int id, int? page)
        {
            try
            {
                page = page.HasValue && page.Value > 0 ? page.Value : 1;
                if (page == null) page = 1; ;
                ViewData["Keyword"] = eDataContext.Tags.Where(t => t.ID == id).FirstOrDefault().Keyword;
                var pageSize = Convert.ToInt32(UserState.getUserStateFromRequest("project.PageSize", "pageSize", Settings.Default.ProductPageSize));
                string order = UserState.getUserStateFromRequest("project.order", "order", "ID").ToString();
                string orderDir = UserState.getUserStateFromRequest("project.orderDir", "orderDir", "desc").ToString();

                ProjectIndexModels p = new ProjectIndexModels();
                p.ImageUrl = Settings.Default.SiteRootUrl + Settings.Default.ProjectRootUrl;
                p.ImagePath = Settings.Default.SiteRootPath + Settings.Default.ProjectRootPath;

                p.Projects = eDataContext.ProjectTagMaps.Where(pr => pr.TagID == id)
                                         .Where(pr => pr.Project.PubYN == true)
                                         .OrderBy("Project.AddDate Desc")
                                         .Select(pr => pr.Project)
                                         .ToPagedList(page.Value - 1, pageSize);
                return View(p);
            }
            catch (Exception exp)
            {
                ErrorLog.WriteLog(exp);
                return View();
            }
        }

        /// <summary>
        /// Author: Takamaru Nguyen
        /// Date: 11/08/2011
        /// </summary>
        [ChildActionOnly]
        public ActionResult TagLasted(int id, string title)
        {
            ViewData["title"] = title;
            List<Tag> lTag = eDataContext.ProjectTagMaps.Where(pt => pt.ProjectID == id)
                                            .OrderBy("Tag.Keyword desc")
                                            .Select(pt => pt.Tag)
                                            .ToList();

            return View(lTag);
        }
    }
}